//
//  MMGSocketClient+MessageSender.m
//  MyApp
//
//  Created by Don on 2017/8/29.
//  Copyright © 2017年 com.memego. All rights reserved.
//

#import "MMGSocketClient+MessageSender.h"
#import "NSString+MMGJson2Str.h"
#import "NSDate+MMGTimestamp.h"
#import "MMGConstSocketMsgKey.h"

@implementation MMGSocketClient (MessageSender)

//- (void)sendSyncTimeMsg {
//    
//    NSDictionary *dic = @{
//                          kMMG_SOCKET_MSG_TYPE:kMMG_SOCKET_MSG_SYNC_TIME,
//                          kMMG_SOCKET_MSG_TIMESTAMP: @([NSDate mmg_currentTimeStamp]) //时间戳
//                          };
//    NSString *jsonString = [NSString mmg_json2String:dic];
//    DLog(@"sendSyncTime msg is ----%@----",jsonString);
//    NSData *data = [self encodeJsonString:jsonString];//[jsonString dataUsingEncoding:NSUTF8StringEncoding];
//    [self sendMessage:data];
//}

- (NSData *)heartBeatMsg {
    NSDictionary *dic = @{
                          kMMG_SOCKET_MSG_TYPE:kMMG_SOCKET_MSG_HEARTBEAT,
                          kMMG_SOCKET_MSG_TIMESTAMP: @([self nowAffterSyncTimestamp]) //时间戳
                          };
    NSString *jsonString = [NSString mmg_json2String:dic];
    DLog(@"sendHeartBeatMsg msg is ----%@----",jsonString);
    NSData *data = [self encodeJsonString:jsonString];
    return data;
}

//- (void)sendConnectPrizeClawMachineMsg:(NSString *)machinId {
//    
//    NSDictionary *dic = @{
//                          kMMG_SOCKET_MSG_TYPE:kMMG_SOCKET_MSG_CONNECT_DEVICE,
//                          kMMG_SOCKET_MSG_TIMESTAMP: @([self nowAffterSyncTimestamp]), //时间戳
//                          kMMG_SOCKET_MSG_DATA:machinId//kPRIZE_CLAW_ID
//                          };
//    NSString *jsonString = [NSString mmg_json2String:dic];
//    DLog(@"sendConnectPrizeClawMachineMsg msg is ----%@----",jsonString);
//    NSData *data = [self encodeJsonString:jsonString];
////    [self.sendingDataBuffer addObject:data];
//    [self sendMessage:data];
//}

- (void)sendConnectDeviceWithPrizeId:(NSString *)prizeId {
    NSDictionary *dic = @{
                          kMMG_SOCKET_MSG_TYPE:kMMG_SOCKET_MSG_CONNECT_DEVICE,
                          kMMG_SOCKET_MSG_TIMESTAMP: @([self nowAffterSyncTimestamp]), //时间戳
                          kMMG_SOCKET_MSG_DATA:prizeId//kPRIZE__ID
                          };
    NSString *jsonString = [NSString mmg_json2String:dic];
    DLog(@"sendConnectDevice msg is ----%@----",jsonString);
    NSData *data = [self encodeJsonString:jsonString];
    //    [self.sendingDataBuffer addObject:data];
    [self sendMessage:data];

}
- (void)sendUserAuthoMsg:(NSString *)authoCode {
    NSDictionary *dic = @{
                          kMMG_SOCKET_MSG_TYPE:kMMG_SOCKET_MSG_USER_LOGIN,
                          kMMG_SOCKET_MSG_TIMESTAMP: @([self nowAffterSyncTimestamp]), //时间戳
                          kMMG_SOCKET_MSG_DATA:authoCode
                          };
    NSString *jsonString = [NSString mmg_json2String:dic];
    DLog(@"sendUserAuthoMsg msg is ----%@----",jsonString);
    NSData *data = [self encodeJsonString:jsonString];
//    [self.sendingDataBuffer addObject:data];
    [self sendMessage:data];
}//user login

- (void)sendGameReserveMsg {
    NSDictionary *dic = @{
                          kMMG_SOCKET_MSG_TYPE:kMMG_SOCKET_MSG_RESERVE_GAME,
                          kMMG_SOCKET_MSG_TIMESTAMP: @([self nowAffterSyncTimestamp]), //时间戳
                          };
    NSString *jsonString = [NSString mmg_json2String:dic];
    DLog(@"sendGameReadyMsg msg is ----%@----",jsonString);
    NSData *data = [self encodeJsonString:jsonString];
//    [self.sendingDataBuffer addObject:data];
    [self sendMessage:data];
}
- (void)sendGameStartMsg:(NSString *)limitTime {
    NSDictionary *dic = @{
                          kMMG_SOCKET_MSG_TYPE:kMMG_SOCKET_MSG_START_GAME,
                          kMMG_SOCKET_MSG_TIMESTAMP: @([self nowAffterSyncTimestamp]), //时间戳
                          kMMG_SOCKET_MSG_DATA:limitTime
                          };
    NSString *jsonString = [NSString mmg_json2String:dic];
    DLog(@"sendGameStartMsg msg is ----%@----",jsonString);
    NSData *data = [self encodeJsonString:jsonString];
//    [self.sendingDataBuffer addObject:data];
    [self sendMessage:data];
}
- (void)sendPrizeClawMachineControlMsgWith:(NSString *)control {

    NSDictionary *dic = @{
                          kMMG_SOCKET_MSG_TYPE:kMMG_SOCKET_MSG_CONTROL_DEVICE,
                          kMMG_SOCKET_MSG_TIMESTAMP: @([self nowAffterSyncTimestamp]), //时间戳
                          kMMG_SOCKET_MSG_DATA:control
                          };
    NSString *jsonString = [NSString mmg_json2String:dic];
    DLog(@"sendPrizeClawMachineControlMsgWith msg is ----%@----",jsonString);
    NSData *data = [self encodeJsonString:jsonString];
//    [self.sendingDataBuffer addObject:data];
    [self sendMessage:data];
}
- (void)sendStartClawMsg {
    NSDictionary *dic = @{
                          kMMG_SOCKET_MSG_TYPE:kMMG_SOCKET_MSG_DOWN_CLAW,
                          kMMG_SOCKET_MSG_TIMESTAMP: @([self nowAffterSyncTimestamp]) //时间戳
                          };
    NSString *jsonString = [NSString mmg_json2String:dic];
    DLog(@"sendStartClawMsg msg is ----%@----",jsonString);
    NSData *data = [self encodeJsonString:jsonString];
//    [self.sendingDataBuffer addObject:data];
    [self sendMessage:data];
}

- (void)sendWinPrizeMsg {
    NSDictionary *dic = @{
                          kMMG_SOCKET_MSG_TYPE:kMMG_SOCKET_MSG_WIN_PRIZE_ACK,
                          kMMG_SOCKET_MSG_TIMESTAMP: @([self nowAffterSyncTimestamp]) //时间戳
                          };
    NSString *jsonString = [NSString mmg_json2String:dic];
    DLog(@"sendWinPrizeMsg msg is ----%@----",jsonString);
    NSData *data = [self encodeJsonString:jsonString];
//    [self.sendingDataBuffer addObject:data];
    [self sendMessage:data];
}

- (void)sendInAppPurchaseTransMsgWith:(NSString *)transReceipt {
    NSDictionary *dic = @{
                          kMMG_SOCKET_MSG_TYPE:kMMG_SOCKET_MSG_IAP_PAY,
                          kMMG_SOCKET_MSG_TIMESTAMP: @([self nowAffterSyncTimestamp]), //时间戳
                          kMMG_SOCKET_MSG_DATA:transReceipt
                          };
    NSString *jsonString = [NSString mmg_json2String:dic];
    DLog(@"sendInAppPurchaseTransMsgWith msg is ----%@----",jsonString);
    NSData *data = [self encodeJsonString:jsonString];
    [self sendMessage:data];
}


- (NSData *)encodeJsonString:(NSString*)jsonString {
    
    if (NSStringIsNullOrEmpty(jsonString)) {
        return nil;
    }
    DLog(@"jsonString %l",jsonString.length);
    const char *string = [jsonString UTF8String];
    size_t strLen = strlen(string);
    uint8_t sendbuf[strLen + 4];
    int len;
    len = sprintf((char*)sendbuf + 4, "%s", string);
    sendbuf[0] = (uint8_t)((len >> 24));
    sendbuf[1] = (uint8_t)((len >> 16) & 0xff);
    sendbuf[2] = (uint8_t)((len >> 8) & 0xff);
    sendbuf[3] = (uint8_t)(len & 0xff);
    DLog(@"curretn len %d",len);
    NSData *data = [NSData dataWithBytes:sendbuf length:(len+4)];
    
    return data;
}

- (NSTimeInterval)nowAffterSyncTimestamp {
    return [NSDate mmg_currentTimeStamp] + self.syncTimestampDifference;
}
@end
